//
//  CRegistViewController.m
//  coninbase_data
//
//  Created by 2.5 on 2021/9/8.
//  Copyright © 2021 ChinaRainbow-iOS. All rights reserved.
//

#import "CRegistViewController.h"
#import "CHttpManager.h"
@interface CRegistViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)NSMutableArray *tfArray;
@property (nonatomic,strong)UIScrollView *scrollView;

@end

@implementation CRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customeCCUI];
}

-(void)customeCCUI
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.ccwidth, self.view.ccheight)];
    self.scrollView.delegate = self;
    self.scrollView.bounces = YES;
    self.scrollView.contentSize= CGSizeMake(0, self.view.ccheight+10);
    [self.view addSubview:self.scrollView];
    
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllAction)];
    [self.scrollView addGestureRecognizer:tgr];
    
    
    
    NSArray *textArray = @[@"account:",@"password:",@"password:"];
    NSArray *placeArray = @[@"Please enter the account number",@"Please enter the pass",@"Please enter the pass again"];
    
    self.tfArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i<textArray.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 50 + i*100, 90, 40)];
        label.font = [UIFont boldSystemFontOfSize:18];
        label.text = textArray[i];
        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(label.ccwidth+30+10, label.ccy, self.view.ccwidth-label.ccwidth+30+10-30, label.ccheight)];
        tf.placeholder = placeArray[i];
        [self.scrollView addSubview:label];
        [self.scrollView addSubview:tf];
        if(i!=0){
            tf.secureTextEntry = YES;
        }
        [self.tfArray addObject:tf];
    }
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 380, self.view.ccwidth-60, 80)];
    [button setTitle:@"regist" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [button addTarget:self action:@selector(registAction) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:button];

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self hideAllAction];
}
-(void)hideAllAction
{
    for (UITextField *tf  in self.tfArray) {
        [tf resignFirstResponder];
    }
}
-(void)registAction
{
    NSInteger i =0;
    NSString *pass = @"";
    for (UITextField *tf  in self.tfArray) {
        [tf resignFirstResponder];
        if(!tf.text||!tf.text.length){
            UIAlertController *avc =[UIAlertController alertControllerWithTitle:@"alert" message:@"Please enter the all infomation!" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil];
            [avc addAction:cancel];
            [self presentViewController:avc animated:YES completion:nil];
            return;
        }
        if(i==1){
            pass = tf.text;
        }else if(i==2){
            if(![tf.text isEqualToString:pass]){
                UIAlertController *avc =[UIAlertController alertControllerWithTitle:@"alert" message:@"Please enter the same pass!" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleCancel handler:nil];
                [avc addAction:cancel];
                [self presentViewController:avc animated:YES completion:nil];
                return;
            }
        }
        i++;
    }
    UITextField *tf1 = [self.tfArray firstObject];
    UITextField *tf2 = [self.tfArray lastObject];
    NSString *url = [NSString stringWithFormat:@"http://122.10.161.2/api/v4/loginR?username=%@&password=%@&name=user%d&sex=man",tf1.text,tf2.text,arc4random()];
    kWeakSelf;
    [CHttpManager requestGET:url andParam:@{} andSuccess:^(NSDictionary * _Nonnull reslutDic) {
        NSDictionary *dataDic = reslutDic[@"data"];
        if([dataDic isKindOfClass:[NSDictionary class]]){
            for (NSString *key in dataDic.allKeys) {
                [[NSUserDefaults standardUserDefaults] setObject:NotNilStr(dataDic[key]) forKey:key];
            }
        }
        [[NSUserDefaults standardUserDefaults] setObject:tf1.text forKey:@"cctoken"];
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    } andFailed:^(NSError * _Nullable error, NSDictionary * _Nullable resultDic) {
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    }];
}
@end
